/*
 * Copyright 2010 The Scripps Research Institute
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.scripps.fl.color;

import java.awt.Color;
import java.util.*;

import edu.scripps.fl.collections.FuzzyMap;

/**
 * A FuzzyMap subclass for Color gradients. For a numeric value within a given range, a distinct Color is returned.
 * 
 * @author Mark Southern (southern at scripps dot edu).
 */
public class ColorMap extends FuzzyMap<Double, Color> {
	public static ColorMap createFireInstance(int steps, double from, double to) {
		Color[] colors = ColorUtils.getRedtoYellowSpectrum(steps);
		return createInstance(colors, from, to);
	}

	public static ColorMap createGradientInstance(Color c1, Color c2, Color c3, int steps, double from, double to) {
		Color[] colors = ColorUtils.getColorGradient(steps, c1, c2, c3);
		return createInstance(colors, from, to);
	}

	public static ColorMap createGradientInstance(Color c1, Color c2, int steps, double from, double to) {
		Color[] colors = ColorUtils.getColorGradient(steps, c1, c2);
		return createInstance(colors, from, to);
	}

	public static ColorMap createInstance(Color[] colors, double from, double to) {
		ColorMap cMap = new ColorMap();
		for (int ii = 0; ii < colors.length; ii++) {
			double d = from + (((ii) * (to - from)) / (double) colors.length);
			cMap.put(d, colors[ii]);
		}
		return cMap;
	}

	public static ColorMap createInstance(Color[] colors, double[] doubles) {
		Arrays.sort(doubles);
		ColorMap cMap = new ColorMap();
		for (int i = 0; i < Math.min(colors.length, doubles.length); i++) {
			cMap.put(doubles[i], colors[i]);
		}
		return cMap;
	}

	public static ColorMap createRainbowInstance(int steps, double from, double to) {
		Color[] colors = ColorUtils.getRedtoBlueSpectrum(steps);
		return createInstance(colors, from, to);
	}

	// ---------------------------------------------------------------------------------------------------------
}